<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form :inline="true" :model="queryParams" ref="queryForm">
        <el-form-item label="用户姓名">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-card">
      <el-table
          v-loading="loading"
          :data="logList"
          border
          stripe
          style="width: 100%"
      >
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="user_name" label="用户姓名" min-width="120" show-overflow-tooltip />
        <el-table-column prop="login_time" label="登录时间" min-width="160" show-overflow-tooltip />
        <el-table-column prop="creat_time" label="记录时间" min-width="160" show-overflow-tooltip />
        <el-table-column label="操作" width="120" align="center">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加删除确认对话框 -->
    <el-dialog
        title="提示"
        :visible.sync="deleteDialogVisible"
        width="300px"
    >
      <span>确定要删除这条日志记录吗？</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="deleteDialogVisible = false">取消</el-button>
    <el-button type="danger" @click="confirmDelete">确定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import {deleteLoginLog, getLoginLogList} from '@/api/log/loginLog'
import Pagination from '@/components/Pagination'

export default {
  name: "loginLog",
  components: { Pagination },
  filters: {
    // 日期时间格式化
    formatDateTime(time) {
      if (!time) return ''
      const date = new Date(time)
      return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}:${String(date.getSeconds()).padStart(2, '0')}`
    }
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 日志表格数据
      logList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: '',
      },
      deleteDialogVisible: false, // 删除确认框显示状态
      deleteId: null, // 要删除的记录ID
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表数据
    getList() {
      this.loading = true
      const params = {
        ...this.queryParams
      }
      getLoginLogList(params).then(res => {
        this.logList = res.data
        this.loading = false
      }).catch(err => {
        this.loading = false
        console.error('获取登录日志失败:', err)
        this.$message.error('获取登录日志失败')
      })
    },

    // 搜索按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },

    // 重置按钮操作
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.queryParams.timeRange = []
      this.handleQuery()
    },
    // 处理删除按钮点击
    handleDelete(row) {
      this.deleteId = row.id
      this.deleteDialogVisible = true
    },

    // 确认删除
    confirmDelete() {
      let data = {
        id:this.deleteId
      }
      deleteLoginLog(data).then(res => {
        this.$message.success('删除成功')
        this.deleteDialogVisible = false
        this.getList()
      }).catch(err => {
        this.$message.error('删除失败')

      })
    }
  }
}
</script>

<style scoped lang="scss">
.app-container {
  padding: 20px;

  .search-card {
    margin-bottom: 20px;
  }

  .table-card {
    .el-table {
      margin-bottom: 20px;
    }
  }

  .el-form-item {
    margin-bottom: 0;
  }
}
</style>